package com.zhongjian.technologycal.mall.fragment;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.os.Vibrator;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.githang.statusbar.StatusBarCompat;
import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.transformer.AccordionTransformer;
import com.zhongjian.technologycal.mall.R;
import com.zhongjian.technologycal.mall.activity.LoginActivity;
import com.zhongjian.technologycal.mall.activity.main.activity.BusinessCollegeActivity;
import com.zhongjian.technologycal.mall.activity.main.activity.MembersAreaListActivity;
import com.zhongjian.technologycal.mall.activity.main.activity.MembersAreaTagActivity;
import com.zhongjian.technologycal.mall.activity.main.activity.MembersAreaZhongJianActivity;
import com.zhongjian.technologycal.mall.activity.main.activity.ProductsDetailsActivity;
import com.zhongjian.technologycal.mall.activity.main.activity.SearchDataActivity;
import com.zhongjian.technologycal.mall.activity.main.activity.SimpleCaptureActivity;
import com.zhongjian.technologycal.mall.activity.main.activity.SystemMessageActivity;
import com.zhongjian.technologycal.mall.activity.personal.activity.VipLevelActivity;
import com.zhongjian.technologycal.mall.activity.web.WebViewActivity;
import com.zhongjian.technologycal.mall.activity.web.WebViewMarkActivity;
import com.zhongjian.technologycal.mall.adapter.HomeGridAdapter;
import com.zhongjian.technologycal.mall.adapter.HomeGridViewAdapter;
import com.zhongjian.technologycal.mall.base.BaseContacts;
import com.zhongjian.technologycal.mall.base.BaseFragment;
import com.zhongjian.technologycal.mall.base.BaseRebPacketBean;
import com.zhongjian.technologycal.mall.bean.BaseBean;
import com.zhongjian.technologycal.mall.bean.HomeBean;
import com.zhongjian.technologycal.mall.net.ApiRetrofitClient;
import com.zhongjian.technologycal.mall.net.HttpRetrofitClient;
import com.zhongjian.technologycal.mall.utils.AppTools;
import com.zhongjian.technologycal.mall.utils.CommonDialog;
import com.zhongjian.technologycal.mall.utils.PicassoImageLoader;
import com.zhongjian.technologycal.mall.utils.LoadingDialog;
import com.zhongjian.technologycal.mall.utils.NetWorkUtils;
import com.zhongjian.technologycal.mall.utils.ResultDialog;
import com.zhongjian.technologycal.mall.utils.ShakeHelper;
import com.zhongjian.technologycal.mall.utils.ToastUitl;
import com.zhongjian.technologycal.mall.utils.UserUtils;
import com.zhongjian.technologycal.mall.view.DataSetAdapter;
import com.zhongjian.technologycal.mall.view.GridViewForScrollView;
import com.zhongjian.technologycal.mall.view.VerticalRollingTextView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;

import butterknife.BindView;
import butterknife.OnClick;
import me.drakeet.materialdialog.MaterialDialog;
import okhttp3.ResponseBody;
import rx.Subscriber;

import static android.content.Context.VIBRATOR_SERVICE;

/**
 * Created by wei on 2017/10/20.
 * 首页功能的实现
 */

public class HomeFragment extends BaseFragment {
    @BindView(R.id.current_list)
    GridViewForScrollView listView;
    @BindView(R.id.current_list1)
    GridViewForScrollView listView1;
    @BindView(R.id.scrollView_home)
    ScrollView scrollView_home;
    @BindView(R.id.img_qrcode)
    ImageView img_qrcode;
    @BindView(R.id.refreshLayout)
    RefreshLayout refreshLayout;
    @BindView(R.id.verticalRollingView)
    VerticalRollingTextView verticalRollingTextView;
    @BindView(R.id.banner)
    Banner banner;
    @BindView(R.id.iv_system_message)
    ImageView iv_system_message;
    @BindView(R.id.home_tv_title_name)
    TextView home_tv_title_name;
    @BindView(R.id.home_tv_title_one_name)
    TextView home_tv_title_one_name;
    @BindView(R.id.home_ll_title_one_name)
    LinearLayout home_ll_title_one_name;

    private List<HomeBean> listItems = new ArrayList<>();
    private List<HomeBean> listItems1 = new ArrayList<>();
    private HomeGridAdapter homeGridAdapter;
    private HomeGridViewAdapter homeGridViewAdapter;
    private List<String> aList = new ArrayList<>();
    private List<String> aListAdv = new ArrayList<>();
    private List<HomeBean> banners = new ArrayList<>();
    private int tag0;
    private int tag1;
    private int pageNum = 50;
    private int sNum;
    private CommonDialog commonDialog = null;

    private String mTitle0;
    private String mTitle1;
    private ShakeHelper shakeHelper;
    private Vibrator vibrator = null;
    private String handRedPacket;
    public static boolean ishand = true;
    private Dialog dialog = null;
    private ImageView imageView_redpacket;
    private TextView redPacket_tv_money;
    private TextView textView_text;
    ReentrantLock lock = new ReentrantLock();
    private MaterialDialog mMaterialDialog = null;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    if (homeGridAdapter != null) {
                        homeGridAdapter.notifyDataSetChanged();
                    }
                    break;
                case 1:
                    if (homeGridViewAdapter != null) {
                        homeGridViewAdapter.notifyDataSetChanged();
                    }

                    break;
            }
        }
    };

    @Override
    public void initPresenter() {
        initRequestVersion();
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_location;
    }

    @Override
    protected void initView() {
        StatusBarCompat.setStatusBarColor(getActivity(), getResources().getColor(R.color.border_color), true);
        requesHomePage();
        shakeListenerCheckRed();
        scrollView_home.post(new Runnable() {
            @Override
            public void run() {
                scrollView_home.scrollTo(0,0);
                scrollView_home.smoothScrollTo(0, 0);
            }
        });

        listView.setFocusable(false);
        refreshLayout.setHeaderMaxDragRate(10);
        refreshLayout.setEnableLoadmore(false);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                requesHomePage();
                refreshlayout.finishRefresh();
            }
        });
        if (!NetWorkUtils.isNetConnected(getActivity())) {
            ToastUitl.showShort(R.string.no_net);
            return;
        } else {
            initshowDialog();
            homeGridAdapter = new HomeGridAdapter(getActivity(), listItems);
            listView.setAdapter(homeGridAdapter);
            homeGridViewAdapter = new HomeGridViewAdapter(getActivity(), listItems1);
            listView1.setAdapter(homeGridViewAdapter);

            verticalRollingTextView.setOnItemClickListener(new VerticalRollingTextView.OnItemClickListener() {
                @Override
                public void onItemClick(VerticalRollingTextView view, int index) {
//                Toast.makeText(getActivity(), "" + index, Toast.LENGTH_SHORT).show();

                }
            });

        }

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intentOther = new Intent();
                intentOther.setClass(getActivity(), ProductsDetailsActivity.class);
                intentOther.putExtra("productId", String.valueOf(listItems.get(i).getData()
                        .getProducts().get(0).getProductsOfTag().get(i).getId()));
                startActivity(intentOther);
            }
        });

        listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intentOther = new Intent();
                intentOther.setClass(getActivity(), ProductsDetailsActivity.class);
                intentOther.putExtra("productId", String.valueOf(listItems1.get(i).getData()
                        .getProducts().get(1).getProductsOfTag().get(i).getId()));
                startActivity(intentOther);
            }
        });

    }
    /**
     * 摇一摇震动抢红包
     */
    int mShakeCount = 0;

    private void shakeListenerCheckRed() {
        shakeHelper = new ShakeHelper(getActivity(), new ShakeHelper.ShakeListener() {
            @Override
            public void shakeListener() {
                mShakeCount++;
                if (dialog == null) {

                } else {
                    if (dialog.isShowing()) {

                    } else {
                        ishand = true;
                    }
                }
                Log.i("ggg", "你摇晃了手机");
                if (ishand == true) {
                    ishand = false;
                    if (UserUtils.getUserToken() == null || UserUtils.getUserToken().equals("")) {
                        Intent intent = new Intent();
                        intent.setClass(getActivity(), LoginActivity.class);
                        startActivity(intent);
                    } else {
                        initRequestCheckRedPacketKey();
                    }

                } else {

                }


            }
        });
    }


    @Override
    public void startActivity(Class<?> cls) {
        super.startActivity(cls);

    }

    @Override
    public void onStart() {
        super.onStart();
        banner.startAutoPlay();
    }

    @Override
    public void onStop() {
        super.onStop();
        banner.stopAutoPlay();
        if (verticalRollingTextView.isRunning()) {
            verticalRollingTextView.stop();
        }

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

        if (vibrator != null) {
            vibrator = null;
        }
        if (shakeHelper != null) {
            shakeHelper.Stop();
        }

    }


    /**
     * 扫一扫
     */
    @OnClick(R.id.img_qrcode)
    public void appQrCode() {
        Intent intent = new Intent(getActivity(), SimpleCaptureActivity.class);
        startActivityForResult(intent, 1);
    }

    /**
     * 新人专享
     */

    @OnClick(R.id.home_ll_exclusive)
    public void onClickExclusive() {
        if (UserUtils.getUserToken() == null || UserUtils.getUserToken().equals("")) {
            Intent intent = new Intent();
            intent.setClass(getActivity(), LoginActivity.class);
            startActivity(intent);
        } else {
            Intent intent = new Intent();
            intent.setClass(getActivity(), WebViewActivity.class);
            intent.putExtra("url_host", BaseContacts.URL_NEW_SHARA);
            startActivity(intent);
        }

    }

    /**
     * 签到好礼
     */
    @OnClick(R.id.home_ll_sign)
    public void onClickSign() {
        if (UserUtils.getUserToken() == null || UserUtils.getUserToken().equals("")) {
            Intent intent = new Intent();
            intent.setClass(getActivity(), LoginActivity.class);
            startActivity(intent);
        } else {
            Intent intent = new Intent();
            intent.setClass(getActivity(), WebViewActivity.class);
            intent.putExtra("url_host", BaseContacts.URL_NEW_Sign);
            startActivity(intent);
        }

    }

    /**
     * 会员专区
     */
    @OnClick(R.id.home_ll_donation)
    public void onClickDonation() {
        Intent intent = new Intent(getActivity(), MembersAreaListActivity.class);
        startActivity(intent);
    }

    /**
     * 商学院
     */
    @OnClick(R.id.home_ll_college)
    public void onClickCollege() {
        Intent intent = new Intent(getActivity(), BusinessCollegeActivity.class);
        startActivity(intent);
    }

    /**
     * 搜索
     */
    @OnClick(R.id.et_search)
    public void onClickSeacher() {
        Intent intent = new Intent(getActivity(), SearchDataActivity.class);
        startActivityForResult(intent, 1);
    }

    /**
     * 众健商城专区
     */
    @OnClick(R.id.home_tv_title_name)
    public void onClickTitleName() {
        Intent intent = new Intent(getActivity(), MembersAreaZhongJianActivity.class);
        intent.putExtra("tag", String.valueOf(tag0));
        intent.putExtra("mTitle", mTitle0);
        startActivity(intent);
    }

    /**
     *
     */
    @OnClick(R.id.home_tv_title_one_name)
    public void onClickTitleOneName() {
        Intent intent = new Intent(getActivity(), MembersAreaTagActivity.class);
        intent.putExtra("tag", String.valueOf(tag1));
        intent.putExtra("mTitle", mTitle1);
        startActivity(intent);
    }

    /**
     * 跳转系统消息
     */
    @OnClick(R.id.ll_system_message)
    public void appClickOpenSystemMessage() {
        Intent intent = new Intent(getActivity(), SystemMessageActivity.class);
        startActivity(intent);
    }

    /**
     * 首页数据的请求
     */
    private void requesHomePage() {
        if (!NetWorkUtils.isNetConnected(getActivity())) {
            ToastUitl.showShort(R.string.no_net);
            return;
        }
        HttpRetrofitClient.getInstance(getActivity()).getUrlRequest(pageNum, new Subscriber<ResponseBody>() {
            @Override
            public void onStart() {
                super.onStart();
            }

            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(ResponseBody responseBody) {
                try {
                    String result = responseBody.string();
                    Gson gson = new Gson();
                    HomeBean homeBean = gson.fromJson(result, HomeBean.class);
                    aList.clear();
                    aListAdv.clear();
                    banners.clear();
                    if (homeBean.getError_code() == 0) {
                        for (int i = 0; i < homeBean.getData().getPicList().size(); i++) {
                            aList.add(BaseContacts.BASE_URL + homeBean.getData().getPicList().get(i).getPic());
//                                aListAdv.add(String.valueOf(homeBean.getData().getPicList().get(i).getAdvType()));
                            banners.add(homeBean);
                        }
                        /**
                         * 设置轮播图填充以及点击事件处理
                         */
                        banner.setImages(aList)
                                .setImageLoader(new PicassoImageLoader())
                                .setOnBannerListener(new OnBannerListener() {
                                    @Override
                                    public void OnBannerClick(int position) {
                                        if (banners.get(position).getData().getPicList().get(position).getAdvType() == 3) {
                                            if (UserUtils.getUserToken() == null || UserUtils.getUserToken().equals("")) {
                                                Intent intent = new Intent();
                                                intent.setClass(getActivity(), LoginActivity.class);
                                                startActivity(intent);
                                            } else {
                                                Intent intent = new Intent();
                                                intent.setClass(getActivity(), VipLevelActivity.class);
                                                startActivity(intent);
                                            }

                                        } else if (banners.get(position).getData().getPicList().get(position).getAdvType() == 2) {
                                            Intent intentOther = new Intent();
                                            intentOther.setClass(getActivity(), ProductsDetailsActivity.class);
                                            intentOther.putExtra("productId", banners.get(position).getData().getPicList().get(position).getProductId());
                                            startActivity(intentOther);

                                        } else if (banners.get(position).getData().getPicList().get(position).getAdvType() == 1) {
                                            if (UserUtils.getUserToken() == null || UserUtils.getUserToken().equals("")) {
                                                Intent intent = new Intent();
                                                intent.setClass(getActivity(), LoginActivity.class);
                                                startActivity(intent);
                                            } else {
                                                if (banners.get(position).getData().getPicList().get(position).getUrl().equals("")
                                                        || TextUtils.isEmpty(banners.get(position).getData().getPicList().get(position).getUrl())) {

                                                } else {
                                                    Intent intent = new Intent();
                                                    intent.setClass(getActivity(), WebViewMarkActivity.class);
                                                    intent.putExtra("url", banners.get(position).getData().getPicList().get(position).getUrl());
                                                    startActivity(intent);
                                                }
                                            }
                                        }


                                    }
                                })
                                .start();
                        banner.setBannerAnimation(AccordionTransformer.class);
                        home_tv_title_name.setText(homeBean.getData().getProducts().get(0).getTagName());
//
                        tag0 = homeBean.getData().getProducts().get(0).getTagId();
//
                        mTitle0 = homeBean.getData().getProducts().get(0).getTagName();
                        if (homeBean.getData().getProducts().size() > 1) {
                            home_tv_title_one_name.setText(homeBean.getData().getProducts().get(1).getTagName());
                            mTitle1 = homeBean.getData().getProducts().get(1).getTagName();
                            tag1 = homeBean.getData().getProducts().get(1).getTagId();
                            home_ll_title_one_name.setVisibility(View.VISIBLE);
                        } else {
                            home_ll_title_one_name.setVisibility(View.GONE);
                        }

                        List<HomeBean> forList = new ArrayList<>();
                        for (int c = 0; c < homeBean.getData().getProducts().get(0).getProductsOfTag().size(); c++) {
                            forList.add(homeBean);
                        }
                        setData0(forList);
                        List<HomeBean> foreList0 = new ArrayList<>();
                        if (homeBean.getData().getProducts().size() > 1) {
                            if (homeBean.getData().getProducts().get(1).getProductsOfTag().size() > 0) {
                                for (int d = 0; d < homeBean.getData().getProducts().get(1).getProductsOfTag().size(); d++) {
                                    foreList0.add(homeBean);
                                }
                                setData1(foreList0);
                            }
                        }

                        String str = homeBean.getData().getTile().getTitle();
                        String[] stitle = str.split("\\|");

                        verticalRollingTextView.setDataSetAdapter(new DataSetAdapter<String>(Arrays.asList(stitle)) {

                            @Override
                            protected String text(String s) {
                                return s;
                            }
                        });
                        verticalRollingTextView.run();
                    }

                } catch (Exception e) {
                    e.printStackTrace();

                }


            }
        });

    }

    private void setData0(List<HomeBean> foreshowList) {
        if (listItems.size() > 0) {
            listItems.clear();
        }
        for (HomeBean vo : foreshowList) {
            listItems.add(vo);
        }
        handler.sendEmptyMessage(0);

    }

    private void setData1(List<HomeBean> showList) {
        if (listItems1.size() > 0) {
            listItems1.clear();
        }

        for (HomeBean vo : showList) {
            listItems1.add(vo);
        }
        handler.sendEmptyMessage(1);
    }

    private void initshowDialog() {
        if(commonDialog != null && commonDialog.initDialog("").isShowing()){
//            do nothing
        }else{
            commonDialog = new CommonDialog(getActivity());
            commonDialog.setPositiveListener(new CommonDialog.DialogPositiveListener() {
                @Override
                public void onClick() {
                    Intent intent = new Intent();
                    intent.setClass(getActivity(), WebViewMarkActivity.class);
                    intent.putExtra("url", BaseContacts.URL_MINE_FANS);
                    startActivity(intent);
                }
            });
            commonDialog.setNegativeListener(new CommonDialog.DialogNegativeListener() {
                @Override
                public void onClick() {

                }
            });
            commonDialog.initDialog("").show();

        }


    }

    /**
     * 显示dialog
     */
    private void initshowDialog1() {
        if (dialog != null && dialog.isShowing()) {
            //A dialog is already open, wait for it to be dismissed, do nothing

        } else {
            View view = LayoutInflater.from(getActivity()).inflate(
                    R.layout.pwindow_dialog_red, null);
            view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT));
            dialog = ResultDialog.creatAlertDialog(getActivity(), view);
            Window window = dialog.getWindow();
            window.setGravity(Gravity.CENTER);  //此处可以设置dialog显示的位置
//        window.setWindowAnimations(R.style.mystyle);
            window.setWindowAnimations(R.style.popupWindowAnimation10);
            imageView_redpacket = view.findViewById(R.id.imageView_redpacket);
            redPacket_tv_money = view.findViewById(R.id.redPacket_tv_money);
            textView_text = view.findViewById(R.id.textView_text);
            //点击抢红包
            imageView_redpacket.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    initRequestdrawRedPacket(handRedPacket);

                }
            });
            dialog.show();
        }


    }


    private void vibrationPhone() {
        vibrator = (Vibrator) getActivity().getSystemService(VIBRATOR_SERVICE);
        getActivity().getSystemService(VIBRATOR_SERVICE);
        //震动时间
        vibrator.vibrate(1000);
    }

    /**
     * 检查版本号
     */
    private void initRequestVersion() {
        if (!NetWorkUtils.isNetConnected(getActivity())) {
            ToastUitl.showShort(R.string.no_net);
            return;
        }
        ApiRetrofitClient.getInstance(getActivity())
                .getUrlRequestVersion(AppTools.getVersionName(getActivity()), new Subscriber<ResponseBody>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(ResponseBody responseBody) {
                        try {
                            Gson gson = new Gson();
                            BaseBean baseBean = gson.fromJson(responseBody.string(), BaseBean.class);
                            if (baseBean.getError_code() == 0) {

//                        ToastUitl.showShort(""+ baseBean.getError_message());

                            } else if (baseBean.getError_code() == -1) {
                                mMaterialDialog = new MaterialDialog(getActivity())
                                        .setMessage(R.string.app_update_info)
                                        .setPositiveButton("确定", new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                Intent intent = new Intent();
                                                intent.setAction("android.intent.action.VIEW");
                                                Uri content_url = Uri.parse(BaseContacts.URL_MINE_DONWLOADING_APP);
                                                intent.setData(content_url);
                                                startActivity(intent);
                                                mMaterialDialog.dismiss();
                                            }
                                        });
                                mMaterialDialog.setCanceledOnTouchOutside(false);
                                mMaterialDialog.show();
                            } else if (baseBean.getError_code() == 1) {
                                mMaterialDialog = new MaterialDialog(getActivity())
                                        .setMessage(R.string.app_update_info)
                                        .setPositiveButton("确定", new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                Intent intent = new Intent();
                                                intent.setAction("android.intent.action.VIEW");
                                                Uri content_url = Uri.parse(BaseContacts.URL_MINE_DONWLOADING_APP);
                                                intent.setData(content_url);
                                                startActivity(intent);
                                                mMaterialDialog.dismiss();
                                            }
                                        })
                                        .setNegativeButton("取消", new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                mMaterialDialog.dismiss();
                                            }
                                        });
                                mMaterialDialog.setCanceledOnTouchOutside(true);
                                mMaterialDialog.show();
                            } else {
                                ToastUitl.showShort(baseBean.getError_message());
                            }


                        } catch (Exception e) {
                            e.printStackTrace();
                        }


                    }
                });


    }

    /**
     * 检查是否有红包
     */
    private void initRequestCheckRedPacketKey() {
        if (!NetWorkUtils.isNetConnected(getActivity())) {
            ToastUitl.showShort(R.string.no_net);
            return;
        }
        ApiRetrofitClient.getInstance(getActivity()).getUrlRequestGetBeginningRedPacketKey(UserUtils.getUserToken(),
                new Subscriber<ResponseBody>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                    }

                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(ResponseBody responseBody) {
                        try {
                            Gson gson = new Gson();
                            BaseRebPacketBean baseBean = gson.fromJson(responseBody.string(), BaseRebPacketBean.class);
                            if (baseBean.getError_code() == 0) {
//                                ishand = false;
                                handRedPacket = baseBean.getData();
                                vibrationPhone();
//                                showRedIcon();
                                initshowDialog1();
                            } else if (baseBean.getError_code() == -1) {
                                ishand = true;
                                ToastUitl.showShort(baseBean.getError_message());
                            } else {
                                ishand = true;
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                });

    }

    /**
     * 抢红包
     */
    private void initRequestdrawRedPacket(String redPacketKey) {
        if (!NetWorkUtils.isNetConnected(getActivity())) {
            ToastUitl.showShort(R.string.no_net);
            return;

        }
        ApiRetrofitClient.getInstance(getActivity()).postUrlRequestdrawRedPacket(
                UserUtils.getUserToken(), redPacketKey,
                new Subscriber<ResponseBody>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                        LoadingDialog.showDialogForLoading(getActivity(),"正在疯狂抢...",true);
                    }

                    @Override
                    public void onCompleted() {
                        LoadingDialog.cancelDialogForLoading();

                    }

                    @Override
                    public void onError(Throwable e) {
                        LoadingDialog.cancelDialogForLoading();
                        ToastUitl.showShort(e.getMessage());
                    }

                    @Override
                    public void onNext(ResponseBody responseBody) {
                        LoadingDialog.cancelDialogForLoading();
                        try {
                            Gson gson = new Gson();
                            BaseRebPacketBean baseBean = gson.fromJson(responseBody.string(), BaseRebPacketBean.class);
                            if (baseBean.getError_code() == 0) {
                                redPacket_tv_money.setVisibility(View.VISIBLE);
                                textView_text.setVisibility(View.VISIBLE);
                                imageView_redpacket.setVisibility(View.GONE);
                                redPacket_tv_money.setText("" + baseBean.getData());
                            } else if (baseBean.getError_code() == 3) {
                                Intent intent = new Intent();
                                intent.setAction("com.broadcast.info_out_line");
                                getActivity().sendBroadcast(intent);
                            } else {
                                ToastUitl.showShort(baseBean.getError_message());
                                redPacket_tv_money.setVisibility(View.VISIBLE);
                                textView_text.setVisibility(View.GONE);
                                imageView_redpacket.setVisibility(View.GONE);
                                redPacket_tv_money.setText(baseBean.getError_message());
                                redPacket_tv_money.setTextColor(Color.WHITE);
                                redPacket_tv_money.setTextSize(12);
                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                });


    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        ishand = true;
    }

    @Override
    public void onPause() {
        super.onPause();
    }

}
